//
//  NotificationController.m
//  YoHoHo
//
//  Created by Stefan Nestorov on 4/1/15.
//  Copyright (c) 2015 Stefan Nestorov. All rights reserved.
//

#import "NotificationController.h"
#import <UIKit/UIKit.h>
#import "Globals.h"
#import "CommunicationManager.h"
#import "HeroInfo.h"

@implementation NotificationController


+ (instancetype)sharedInstance
{
    
    static NotificationController *instance = nil;
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    
    return instance;

}

- (instancetype)init
{
    
    self = [super init];
    
    if ( self ) {
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationBecameActive) name:UIApplicationDidBecomeActiveNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationBecameInactive) name:UIApplicationWillResignActiveNotification object:nil];
            
    }

    return self;
    
}

- (void)applicationBecameActive
{
    
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    
    NSLog(@"Cancel notifications");
    
}

- (void)applicationBecameInactive
{
    
//    [[CommunicationManager sharedManager] disconnect];
    
    if ( YES ) { //[[NSUserDefaults standardUserDefaults] boolForKey:IS_NOTIFICATIONS_ON]
        
        NSString *chosenHeroName = [HeroInfo getChosenHeroInfo].name;
        
        //Schedule come back notifications
        // 23 hour
        UILocalNotification* notification23 = [[UILocalNotification alloc] init];
        notification23.fireDate = [NSDate dateWithTimeIntervalSinceNow:23*60*60];
        notification23.alertBody = [NSString stringWithFormat:@"%@: \"I miss you 😔. Come play with me!\"", chosenHeroName];
        notification23.timeZone = [NSTimeZone defaultTimeZone];
        notification23.soundName = UILocalNotificationDefaultSoundName;
        [[UIApplication sharedApplication] scheduleLocalNotification:notification23];
        
        //48 hour
        UILocalNotification* notification48 = [[UILocalNotification alloc] init];
        notification48.fireDate = [NSDate dateWithTimeIntervalSinceNow:48*60*60];
        notification48.alertBody = [NSString stringWithFormat:@"%@: \"Let's play together!😊\"", chosenHeroName];
        notification48.timeZone = [NSTimeZone defaultTimeZone];
        notification48.soundName = UILocalNotificationDefaultSoundName;
        [[UIApplication sharedApplication] scheduleLocalNotification:notification48];
        
        //72 hour
        UILocalNotification* notification72 = [[UILocalNotification alloc] init];
        notification72.fireDate = [NSDate dateWithTimeIntervalSinceNow:72*60*60];
        notification72.alertBody = [NSString stringWithFormat:@"%@: \"Let's throw some 💣💣💣\"", chosenHeroName];
        notification72.timeZone = [NSTimeZone defaultTimeZone];
        notification72.soundName = UILocalNotificationDefaultSoundName;
        [[UIApplication sharedApplication] scheduleLocalNotification:notification72];
        
        NSLog(@"Notifications scheduled");
    }
    
}


- (void)dealloc
{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    
}


@end
